<template>
  <el-form ref="deptForm" :model="form" :rules="formRules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getSimple } from '@/api/getsimpie'
// 接口请求 新增 - 部门信息 - 修改部门
import { upDepartments, lodDepartments, upDateDepartments } from '@/api/departments'
export default {
  props: {
    iEid: { type: String, required: true },
    isedit: { type: Boolean, required: true },
    origonList: { type: Array, required: true }
  },
  data() {
    // 校验 - 部门编码
    const vlidaCadeList = (rule, value, callback) => {
      let validres = this.origonList.map(item => item.code)
      if (this.isedit) {
        validres = this.origonList.filter(item => item.id !== this.iEid).map(item => item.code)
        // console.log('validres', validres)
      }
      // 重复部门编码抛出错误
      if (validres.includes(value)) {
        callback(new Error(`部门编码${value}重复,请从新填写!`))
      } else {
        callback()
      }
    //   console.log('this.origonList', validres)
    }
    // 校验 - 部门名称
    const vlidaNameList = (rule, value, callback) => {
      let valiNameList = this.origonList.filter(item => item.pid === this.iEid).map(item => item.name)
      // 解决编辑兄弟级别重复
      if (this.isedit) {
        const pid = this.origonList.find(item => item.id === this.iEid).pid
        const xiondi = this.origonList.filter(item => item.pid === pid)
        valiNameList = xiondi.filter(item => item.id !== this.iEid).map(item => item.name)
      }
      // 重复部门名称抛出错误
      if (valiNameList.includes(value)) {
        callback(new Error(`部门名称${value}重复,请更换!`))
      } else {
        callback()
      }
    //   console.log(valiNameList)
    }
    return {
      list: [], // 存储部门负责人
      // 表单数据
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      },
      // 表单校验
      formRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: vlidaNameList, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: vlidaCadeList, trigeer: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.logSimple() // 获取数据
    if (this.isedit) {
      this.logDateDepartments()
    }
  },
  methods: {
    // 获取部门信息
    async logDateDepartments() {
      try {
        const { data: res } = await lodDepartments(this.iEid)
        // console.log('获取部门', res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 修改部门
    async upAdd() {
      try {
        await upDateDepartments(this.form)
        this.$emit('hsuesser') // 传父关闭弹框
      } catch (error) {
        console.log(error)
      }
    },
    // 新增部门
    async doAdd() {
      try {
        const res = await upDepartments({ pid: this.iEid, ...this.form })
        console.log(res)
        this.$emit('suesser') // 传父关闭弹框
      } catch (error) {
        console.log(error)
      }
    },
    // 弹框确定
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.isedit ? this.upAdd() : this.doAdd() // 触发新增部门
      })
    },
    // 弹框取消
    hCancel() {
      this.$emit('hCancel')
    },
    // 发送请求获取部门负责人
    async logSimple() {
      try {
        const { data: res } = await getSimple()
        // console.log(res)
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
